package com.charging.quartz.task;

import com.charging.common.constant.Constants;
import com.charging.system.domain.KxChargingRecord;
import com.charging.system.service.IKxChargingRecordService;
import org.springframework.stereotype.Component;
import com.charging.common.utils.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 定时任务调度测试
 *
 * @author charging
 */
@Component("chargingTask")
public class ChargingTask
{
    public void chargingMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void chargingParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void chargingNoParams()
    {
        System.out.println("执行无参方法");
    }

    private static ExecutorService executor = Executors.newCachedThreadPool();
    private static List<Future<?>> futures = new ArrayList<Future<?>>();

    private static boolean isPrintLog = false;	// 根据自定义打印间隔时间，判断是否该打印日志
    // 如果定时器检测定义时间大于5秒，就会以定时器的时间为准。
    private static long printPeriod = 5;		// 自定义日志打印间隔时间（单位：秒）
    private static long printTime = 0;			// 上次打印时间
    @Resource
    private IKxChargingRecordService kxChargingRecordService;

    /**
     * 每分钟计费一次
     */
    public void Charging(){
        //自定义打印日志间隔时间
        this.isPrintLogHandle();
        KxChargingRecord kxChargingRecord = new KxChargingRecord();
        kxChargingRecord.setChargingStatus(Constants.ChargingStatus.INTCHARGING);
        kxChargingRecord.setEquipType(1);//科航二轮桩
        List<KxChargingRecord> kxChargingRecords = kxChargingRecordService.selectKxChargingRecordList(kxChargingRecord);
        kxChargingRecordService.chargingAction(kxChargingRecords);//执行计费逻辑
    }
    /**
     * 根据自定义打印间隔时间，判断是否该打印日志
     */
    private void isPrintLogHandle(){
        long curTime = (new Date().getTime()/1000);
        isPrintLog = ((curTime + 1) > (printTime + printPeriod));
        if(isPrintLog){
            printTime = curTime;
        }
    }
}
